/*
 * parserclass.c
 *
 *  Created on: 2013-5-15
 *      Author: xkaying
 */

#include "parserclass.h"

#include "lexer.h"
#include "../typedef/package.h"
#include "../typedef/prority.h"
#include "../typedef/function.h"
#include "parsertemplate.h"
#include "parserdeclare.h"
#include "parserfunction.h"
#include "parserfoperator.h"


#include <assert.h>
#include <stdlib.h>

#include "parser.h"

int ParserClass(Lexer *lexer, Package *package, int prority)
{
	assert(lexer);
	assert(package);
	int state;
	state = 0;
	ptoken = LexerGetToken(lexer);
	Class *pface;
	pface = ClassInit(prority);
	//默认为protected
	prority = Prority_Protected;
	while (ptoken) {
		switch (state)
		{
			case 0: {
				//名字
				switch (ptoken->type)
				{
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Less: {
						//进入模板读取
						Template *ptmp;
						ptmp = ParserTemplate(lexer);
						if (ptmp) {
							if (!TemplateIsEmpty(ptmp))
								ClassAppendTemplate(pface, ptmp);
							state = 1;
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenType_Variable: {
						ClassAppendName(pface, ptoken->name);
						state = 2;
						continue;
					}
					default: {
						printf("第%d行:类定义格式错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser class状态机错误.状态%d.\n", state);
				return 0;
			}
			case 1: {
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						ClassAppendName(pface, ptoken->name);
						state = 2;
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:类定义格式错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser class状态机错误.状态%d.\n", state);
				return 0;
			}
			case 2: {
				switch (ptoken->type)
				{
					case TokenType_BlockStart: {
						state = 3;
						//进入接口内容读取
						ptoken = LexerGetToken(lexer);
						continue;
					}
						//表示枚举类型定义结束,定义了一个空接口
					case TokenType_End: {
						printf("第%d行:警告 定义了一个空类.\n", ptoken->line);
						PackageAppendClass(package, pface);
						ptoken = LexerGetToken(lexer);
						return 1;
					}
						//读取继承父接口
					case TokenType_Extend: {
						state = 20;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:类定义格式错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser class状态机错误.状态%d.\n", state);
				return 0;
			}
			case 20: {
				//实际内容
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						state = 21;
						ClassAppendSuper(pface, ptoken->name);
						//生成该包
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BlockStart: {
						printf("第%d行:警告 '{'前面多了个':'.(是否漏写了父类或接口)\n");
						state = 3;
						//进入接口内容读取
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:类定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser class状态机错误.状态%d.\n", state);
				return 0;
			}
			case 21: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_Comma: {
						state = 22;
						//生成该包
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BlockStart: {
						state = 3;
						//进入接口内容读取
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:类定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser class状态机错误.状态%d.\n", state);
				return 0;
			}
			case 22: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_Variable: {
						state = 21;
						ClassAppendSuper(pface, ptoken->name);
						//生成该包
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_BlockStart: {
						printf("第%d行:警告 '{'前面多了个',',该符号被忽视.\n");
						state = 3;
						//进入接口内容读取
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:类定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser class状态机错误.状态%d.\n", state);
				return 0;
			}
			case 3: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_BlockEnd: {
						if (ClassIsempty(pface)) {
							printf("警告 定义了一个空类%s.\n", ClassName(pface));
						}
						PackageAppendClass(package, pface);
						ptoken = LexerGetToken(lexer);
						return 1;
					}
					case TokenType_End: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_PUBLIC: {
						state = 4;
						prority = Prority_Public;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_PRIVATE: {
						state = 4;
						prority = Prority_Private;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_PROTECTED: {
						state = 4;
						prority = Prority_Protected;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_CONSTPUBLIC: {
						state = 4;
						prority = Prority_ConstPublic;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_CONSTPRIVATE: {
						state = 4;
						prority = Prority_ConstPrivate;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_CONSTPROTECTED: {
						state = 4;
						prority = Prority_ConstProtected;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_INLINE: {
						state = 5;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_FINAL: {
						state = 32;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						//定义了 declare
						Declare *pdeclare;
						pdeclare=ParserDeclare(lexer, ptoken->name, 0);
						if (pdeclare) {
							ClassAppendDeclare(pface,pdeclare);
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
						//操作符号重载
					case TokenKey_OPERATOR: {
						Function *pfun;
						pfun = FOperatorInit(prority, 0);
						pfun = ParserFOperator(lexer, pfun);
						if (pfun) {
							if (!FunctionIsempty(pfun)) {
								printf("警告:重载操作符时,没有定义重载内容.按照未重载处理.\n");
							}
							ClassAppendFunction(pface, pfun);
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenKey_FUNCTION: {
						Function *pfun;
						pfun = FunctionInit(prority, 0, 0);
						pfun = ParserFunction(lexer, 0);
						if (pfun) {
							if (!FunctionIsempty(pfun)) {
								printf("警告:函数%s沒有定义函数内容.\n",
										FunctionName(pface));
							}
							ClassAppendFunction(pface, pfun);
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误 类内部语句定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser class状态机错误.状态%d.\n", state);
				return 0;
			}
			case 4: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_BlockEnd: {
						printf(
								"警告:类定义结束.但是类中最后一个语句为<public:private:protect:constpublic:constprivate:constprotected>关键字,被自动忽视.\n");
						PackageAppendClass(package, pface);
						ptoken = LexerGetToken(lexer);
						return 1;
					}
					case TokenType_End: {
						printf(
								"第%d行 警告:<public:private:protect:constpublic:constprivate:constprotected>后面跟随';'权限被重新设置为protected.\n",
								ptoken->line);
						prority = Prority_Protected;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Extend: {
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误 接口类型内部接口语句定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser interface状态机错误.状态%d.\n", state);
				return 0;
			}
			case 5: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_End: {
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_FINAL: {
						state = 6;
						ptoken = LexerGetToken(lexer);
						continue;
					}
						//操作符号重载
					case TokenKey_OPERATOR: {
						Function *pfun;
						pfun = FOperatorInit(prority, 1);
						pfun = ParserFOperator(lexer, pfun);
						if (pfun) {
							if (!FunctionIsempty(pfun)) {
								printf("警告:重载操作符时,没有定义重载内容.按照未重载处理.\n");
							}
							state = 3;
							ClassAppendFunction(pface, pfun);
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenKey_FUNCTION: {
						Function *pfun;
						pfun = FunctionInit(prority, 0, 1);
						pfun = ParserFunction(lexer, 0);
						if (pfun) {
							if (!FunctionIsempty(pfun)) {
								printf("警告:函数%s沒有定义函数内容.\n",
										FunctionName(pface));
							}
							state = 3;
							ClassAppendFunction(pface, pfun);
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误 类内部语句定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser class状态机错误.状态%d.\n", state);
				return 0;
			}
			case 6: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_End: {
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_FUNCTION: {
						Function *pfun;
						pfun = FunctionInit(prority, 1, 1);
						pfun = ParserFunction(lexer, 0);
						if (pfun) {
							if (!FunctionIsempty(pfun)) {
								printf("警告:函数%s沒有定义函数内容.\n",
										FunctionName(pface));
							}
							state = 3;
							ClassAppendFunction(pface, pfun);
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误 类内部语句定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser class状态机错误.状态%d.\n", state);
				return 0;
			}
			case 7: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_End: {
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_INLINE: {
						state = 8;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenType_Variable: {
						//定义了 declare
						Declare *pdeclare;
						pdeclare=ParserDeclare(lexer, ptoken->name, 1);
						if (pdeclare) {
							ClassAppendDeclare(pface,pdeclare);
							state = 3;
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenKey_FUNCTION: {
						Function *pfun;
						pfun = FunctionInit(prority, 1, 0);
						pfun = ParserFunction(lexer, 0);
						if (pfun) {
							if (!FunctionIsempty(pfun)) {
								printf("警告:函数%s沒有定义函数内容.\n",
										FunctionName(pface));
							}
							ClassAppendFunction(pface, pfun);
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误 类内部语句定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser class状态机错误.状态%d.\n", state);
				return 0;
			}
			case 8: {
				//枚举的实际内容
				switch (ptoken->type)
				{
					case TokenType_End: {
						state = 3;
						ptoken = LexerGetToken(lexer);
						continue;
					}
					case TokenKey_FUNCTION: {
						Function *pfun;
						pfun = FunctionInit(prority, 1, 1);
						pfun = ParserFunction(lexer, 0);
						if (pfun) {
							if (!FunctionIsempty(pfun)) {
								printf("警告:函数%s沒有定义函数内容.\n",
										FunctionName(pface));
							}
							ClassAppendFunction(pface, pfun);
							continue;
						}
						printf("编译错误.\n");
						return 0;
					}
					case TokenType_Comment: {
						ptoken = LexerGetToken(lexer);
						continue;
					}
					default: {
						printf("第%d行:错误 类内部语句定义错误.\n", ptoken->line);
						return 0;
					}
				}
				printf("parser class状态机错误.状态%d.\n", state);
				return 0;
			}
			default: {
				printf("parser interface状态机错误.状态:default.\n");
				return 0;
			}
		}
	}
	printf("不完整的类,是否缺少}结尾.\n");
	return 0;
	return 0;
}
